package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type User struct {
	ID           uint
	Name         sql.NullString
	Email        *string
	Age          uint8
	Birthday     *time.Time
	MemberNumber sql.NullString
	ActivatedAt  sql.NullTime
	CreatedAt    time.Time
	UpdatedAt    time.Time
}

func main() {
	dsn := "root:root@tcp(192.168.4.111:3306)/mytest?charset=utf8mb4&parseTime=True&loc=Local"
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer（日志输出的目标，前缀和日志包含的内容——译者注）
		logger.Config{
			SlowThreshold: time.Second, // 慢 SQL 阈值
			LogLevel:      logger.Info, // 日志级别
			Colorful:      true,        // 禁用彩色打印
		},
	)

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
	})
	if err != nil {
		fmt.Println("err", err)
	}

	err = db.AutoMigrate(&User{})
	if err != nil {
		fmt.Println("err", err)
	}

	// email := "741883227@qq.com"
	// name := ""

	//db.Create(&User{
	//	Name:     &name,
	//	Email:    &email,
	//	Age:      18,
	//	Birthday: nil,
	//})

	var user User
	t := time.Now()
	db.First(&user, 1)
	db.Model(&user).Updates(User{
		Name: sql.NullString{
			String: "food",
			Valid:  true,
		},
		Birthday: &t,
		ActivatedAt: sql.NullTime{
			Time:  t,
			Valid: true,
		},
		Age: 0,
	})
}
